
#include "combi.h"
#include <stdio.h>

//Programmer: Nguyen Xuan Hoai
//Function: Compute a factorial
//Input: an positive integer number
//Exception Handling: None
//Algorithm: iterative loop of multiplication
long factorial(unsigned n)
{
  unsigned i;
  long fact=1;
   for(i=2;i<=n;i++)
     fact*=i;
   return fact;
}

//Programmer: Nguyen Xuan Hoai
//Function: Compute a combinatorial coefficient
//Input: two positive integer number n and k (where n>=k)
//Exception Handling: if k> n then return 0
//Algorithm: using factorial function C(n,k)= n!/(k!(n-k)!)
long combeff(unsigned n, unsigned k)
{
 if (n>=k) 
  return factorial(n)/(factorial(k)*factorial(n-k));
else
   return 0;
}
//Programmer: Nguyen Xuan Hoai
//Function: Compute a combinatorial coefficient
//Input: two positive integer number n and k (where n>=k)
//Exception Handling: if k> n then return 0
//Algorithm: using iteration C(n,k)= (n/1)*((n-1)/2)*....*(n-k+1)/k)))))
long combeff2(unsigned n, unsigned k)
{
  unsigned i,j,c;
  long comp=1;
  if (k>n)
   return 0;
  else
  {
    i=n;
	j=1;
	for (c=1, i=n,j=1;c<=k;c++,i--,j++)
	  comp=(comp*i)/j;
   return comp;
  
  }
  
}
//Programmer: Nguyen Xuan Hoai
//Function: Print first n rows of the Pascal Triangle
//Input: a positive number n
//Exception Handling: none
//Algorithm: using combinatorial coefficients function 2
void Print_Pascal_Triangle(unsigned n)
{
  unsigned i,j;
  for(i=0;i<n;i++)
  {
   for(j=0;j<=i;j++)
     printf("%ld ",combeff2(i,j));
   printf("\n");
  
  }
}
